package com.ww.web;

import com.alibaba.fastjson.JSON;
import com.ww.dao.DeliveryDao;
import com.ww.dao.GoodsDao;
import com.ww.dao.TrolleyDao;
import com.ww.dao.impl.DeliveryDaoImpl;
import com.ww.dao.impl.GoodsDaoImpl;
import com.ww.dao.impl.TrolleyDaoImpl;
import com.ww.entity.Delivery;
import com.ww.entity.Goods;
import com.ww.entity.Trolley;

import javax.servlet.ServletException;
import javax.servlet.annotation.WebServlet;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import javax.servlet.http.HttpSession;
import java.io.IOException;
import java.io.PrintWriter;
import java.util.ArrayList;
import java.util.List;
import java.util.Map;

@WebServlet("/trolley/*")
public class TrolleyServlet extends HttpServlet {
    private TrolleyDao trolleyDao = new TrolleyDaoImpl();
    private GoodsDao goodsDao = new GoodsDaoImpl();
    private DeliveryDao deliveryDao = new DeliveryDaoImpl();

    @Override
    protected void service(HttpServletRequest req, HttpServletResponse resp) throws ServletException, IOException {
        String baseURL = req.getContextPath();//获取项目的基本路径
        String uri = req.getRequestURI();
        String path = uri.substring(uri.lastIndexOf("/") + 1);
        if ("list.do".equals(path)) {
            HttpSession session = req.getSession();
            String user_id = null;
            if (session.getAttribute("user_id") != null) {
                user_id = session.getAttribute("user_id").toString();
            }
            List<Map<String, Object>> mapList = goodsDao.findAll(user_id);
            req.setAttribute("mapList", mapList);
            req.getRequestDispatcher(baseURL + "/shopcart.jsp").forward(req, resp);
        } else if ("decrease.do".equals(path)) {
            String id = req.getParameter("id");
            Trolley trolley = trolleyDao.findById(id);
            int num = trolley.getTrolley_num() - 1;
            int i = trolleyDao.updateTrolley(id, num);
            resp.setContentType("application/json;charset=utf-8");
            PrintWriter out = resp.getWriter();
            out.write("");
            out.close();
            return;
        } else if ("increase.do".equals(path)) {
            String id = req.getParameter("id");
            Trolley trolley = trolleyDao.findById(id);
            int num = trolley.getTrolley_num() + 1;
            int i = trolleyDao.updateTrolley(id, num);
            resp.setContentType("application/json;charset=utf-8");
            PrintWriter out = resp.getWriter();
            out.write("");
            out.close();
            return;
        } else if ("shopping.do".equals(path)) {
            double salary = Double.parseDouble(req.getParameter("salary"));
            String ids = req.getParameter("id");

            String[] id = ids.split(",");
            List<Map<String, Object>> mapList = new ArrayList<Map<String, Object>>();
            for (String s : id) {
                Map<String, Object> map = trolleyDao.find(s);
                mapList.add(map);
            }
            HttpSession session = req.getSession();
            String user_id = null;
            if (session.getAttribute("user_id") != null) {
                user_id = session.getAttribute("user_id").toString();
            }
            String tid = null;
            if (session.getAttribute("tid") != null) {
                tid= session.getAttribute("tid").toString();
            }
            List<Delivery> deliveryList = deliveryDao.findAll();
            Delivery delivery = deliveryList.get(0);


            if (null != deliveryList) {
                if (null!=tid){
                    delivery = deliveryDao.findById(tid);
                    req.setAttribute("salary", salary);
                    req.setAttribute("delivery", delivery);
                    req.setAttribute("mapList", mapList);
                    req.getRequestDispatcher(baseURL + "/order.jsp").forward(req, resp);
                }else {
                    req.setAttribute("salary", salary);
                    req.setAttribute("delivery", delivery);
                    req.setAttribute("mapList", mapList);
                    req.getRequestDispatcher(baseURL + "/order.jsp").forward(req, resp);
                }




            } else {

                req.setAttribute("mapList", mapList);
                req.setAttribute("salary", salary);
                req.getRequestDispatcher(baseURL + "/tureorder.jsp").forward(req, resp);
            }

            return;
        } else if ("delete.do".equals(path)) {
            String goods_id = req.getParameter("goods_id");
            String user_id = req.getParameter("user_id");
            int i = trolleyDao.deletaT(user_id, goods_id);
            resp.setContentType("application/json;charset=utf-8");
            PrintWriter out = resp.getWriter();
            out.write("");
            out.close();
            return;
        } else if ("tid.do".equals(path)) {
            String tid = req.getParameter("tid");
            req.getSession().setAttribute("tid", tid);
            resp.setContentType("application/json;charset=utf-8");
            PrintWriter out = resp.getWriter();
            out.write("");
            out.close();
            return;
        } else if ("deleteAll.do".equals(path)) {
            String trolley_id = req.getParameter("trolley_id");
            int remove = trolleyDao.delete(trolley_id);
            resp.setContentType("application/json;charset=utf-8");
            PrintWriter out = resp.getWriter();
            out.write("");
            out.close();
            return;
        } else {
            throw new RuntimeException("请求错误");
        }
    }
}
